/*
Prep Data for Model Plots
*/

clear
cd /Users/sophiagilbukh/Dropbox/Research/GGP_Brokers/code/matlab/2023_One_Lag_Poisson_Fix_Office
*global data_dir /Users/sonia/Dropbox/Research/GGP_Brokers/data
*global model_dir /Users/sonia/Dropbox/Research/GGP_Brokers/code/matlab/full_model_one_lag
*global main_dir /Users/sonia/Dropbox/Research/GGP_Brokers/output/figures_one_lag
tempfile temp
tempfile temp2

set more off
set scheme sonia_new

local exp all_experience_l1

** From Data - listings 
foreach i in 1 2 3 {
    if `i'==1 {
		import excel ../save_calibrationcont`exp'.xlsx, sheet("bust") clear 
		xpose, clear
		gen experience = [_n]
		rename v4 sale_prob_adj_data
		drop v*
		gen state = (`i'-1)*3+1
		tempfile temp2
		save `temp2'
	}
	
	else if `i'~=1 {
		import excel ../save_calibrationcont`exp'.xlsx, sheet("bust") clear 
		xpose, clear
		gen experience = [_n]
		rename v4 sale_prob_adj_data
		drop v*
		gen state = (`i'-1)*3+1
		append using `temp2'
		tempfile temp2
		save `temp2'
	}
	
	import excel ../save_calibrationcont`exp'.xlsx, sheet("medium") clear 
	xpose, clear
	gen experience = [_n]
	rename v4 sale_prob_adj_data
	drop v*
	gen state = (`i'-1)*3+2
	append using `temp2'
	tempfile temp2
	save `temp2'
	
	import excel ../save_calibrationcont`exp'.xlsx, sheet("boom") clear 
	xpose, clear
	gen experience = [_n]
	rename v4 sale_prob_adj_data
	drop v*
	gen state = (`i'-1)*3+3
	append using `temp2'
	tempfile temp2
	save `temp2'
}


** From Data - panel
use ../../../data/brokerpanel_main_vars, clear
** learning
by agent: gen learndata = `exp'[_n+1] - `exp' if active[_n+1]==1 & active==1
by agent: gen learndata_alt = num_bs[_n+1] - `exp'
merge m:1 year using ../../../data/StateTransitionExercise/year_states_Ch12
tab _merge
keep if _merge==3
drop _merge
drop t1* t2* t3*

keep entr* exit* *experience* num_* active year main_fips state sale* earnings* learndata*
drop if active==0
gen i=1
bysort state: egen entry = total(entrant)
bysort state: egen num_agents = total(i)
gen entry_rate = entry/num_agents
gen sale_prob_data = sale2/num_list_fc

preserve
collapse learndata* exit_ratedata=exit entrydata=entry_rate profit_data=earnings sale_probdata=sale_prob_data num_bs_data=num_bs listingsdata=num_list (sum) num_agents, by(`exp' state)
replace state = (floor(state/10)-1)*3 + state-10*floor(state/10)
rename `exp' experience
rename num_agents distdata
replace experience = experience+1
merge 1:1 state experience using `temp2'
drop _merge
drop if experience>50

save data_moments`exp', replace

*****************/
	   
	   
** From Model
use data_moments`exp', clear
cd $model_dir
rename sale_probdata sale_prob_data_noadj
rename sale_prob_adj_data sale_probdata

tempfile temp
save `temp'
local files : dir "$model_dir" files "*data.csv"
di `files'
global model

foreach file in `files' {
    local str: subinstr local file "data.csv" ""
	di "`str'"
	if "`str'" ~= "calibration_results_" & "`str'" ~= "commission_split_"  {
	global model $model `str'
	}
}
di "$model"
foreach i in $model {
    di "`i'"
*[State(1,:)' State(3,:)' (Ne+Ne_add)' (Nb+Nb_add)' Sale_p EProfit' policy dist' lambd(State(3,:))' Vs(State(3,:))' Buy_p];
	import delimited using `i'data.csv, clear
	rename v1 experience
	rename v2 state
	rename v3 listings`i'
	rename v4 buyers`i'
	rename v5 sale_prob`i'
	rename v6 profit`i'
	rename v7 exit_rate`i'
	rename v8 dist`i'
	rename v9 entry`i'
	rename v10 sellV`i'
	rename v11 BuyPr`i'	
	
	bysort state: egen num_agents`i' = total(dist`i')
	replace entry`i' = entry`i'/num_agents`i'
	
	gen  density`i' = dist`i'/num_agents`i'
	
	merge 1:1 experience state using `temp'
	drop _merge

	tempfile temp
	save `temp'

}
gen logexp = log(experience)
bysort state: egen num_agentsdata = total(distdata)
gen  densitydata = distdata/num_agentsdata


gen bust=1 if state==1 | state==4 | state==7
gen medium=1 if state==2 | state==5 | state==8
gen boom=1 if state==3 | state==6 | state==9

gen q = 0
replace q = 0.2209 if state==1
replace q = 0.052 if state==2
replace q = 0.065 if state==3
replace q = 0.075 if state==4
replace q = 0.1864 if state==5
replace q = 0.0621 if state==6
replace q = 0.0424 if state==7
replace q = 0.0847 if state==8
replace q = 0.2119 if state==9

save data_model_results`exp', replace
*****************/


************************ Compute All the Relevant Stats *********
use data_model_results`exp', clear
replace experience = experience-1

* Learning
foreach i in $model {
    di "`i'"
    cap restore
    preserve
	keep state experience exit_rate`i'
	rename experience fut_experience
	rename exit_rate`i' fut_exit
	sort state fut_experience
	save $model_dir/exit_rates_model, replace
	restore

    cap drop prob* fut*
	gen buyersbuy`i' = BuyPr`i'*buyers`i'
	gen fut_experience1 = min(49,floor(listings`i'/1)+floor(buyersbuy`i'/1))
	gen prob1 = (1-(listings`i'-floor(listings`i'/1)))*(1-(buyersbuy`i'-floor(buyersbuy`i'/1)))
	gen fut_experience2 = min(49,ceil(listings`i'/1)+floor(buyersbuy`i'/1))
	gen prob2 = (listings`i'-floor(listings`i'/1))*(1-(buyersbuy`i'-floor(buyersbuy`i'/1)))
	gen fut_experience3 = min(49,floor(listings`i'/1)+ceil(buyersbuy`i'/1))
	gen prob3 = (1-(listings`i'-floor(listings`i'/1)))*(buyersbuy`i'-floor(buyersbuy`i'/1))
	gen fut_experience4 = min(49,ceil(listings`i'/1)+ceil(buyersbuy`i'/1))
	gen prob4 = (listings`i'-floor(listings`i'/1))*(buyersbuy`i'-floor(buyersbuy`i'/1))

	foreach j in 1 2 3 4 {
		rename fut_experience`j' fut_experience
		sort state fut_experience
		merge m:1 state fut_experience using $model_dir/exit_rates_model
		drop if _merge==2
		drop _merge
		rename fut_exit fut_exit`j'
		rename fut_experience fut_experience`j'
	}
	gen learn`i' = (prob1*(1-fut_exit1)*fut_experience1 + prob2*(1-fut_exit2)*fut_experience2 + prob3*(1-fut_exit3)*fut_experience3 + prob4*(1-fut_exit4)*fut_experience4)/(prob1*(1-fut_exit1)+prob2*(1-fut_exit2)+prob3*(1-fut_exit3)+prob4*(1-fut_exit4)) - experience
	gen learn`i'alt = (listings`i'+BuyPr`i'*buyers`i') - experience
	}


sort state experience
/*
set more off
foreach i in Baseline $samples {
	gen learn`i' = min(50,listings`i'+BuyPr`i'*buyers`i') - experience
}
*/
* Seller value
foreach i in $model  {		
    gen diffsellV`i' = (sellV`i'-sellVBaseline)*100/sellVBaseline
}

* Employment Difference
foreach i in $model  {		
    gen diffempl`i' = (num_agents`i'-num_agentsBaseline)*100/num_agentsBaseline
}
* rename *ListingDistribution* *LD* 
save data_model_results`exp', replace
